package iterator.v3;

public class ArrayList_<T> implements Collection_<T> {
    T[] data = (T[]) new Object[10];
    int size = 0;

    @Override
    public void add(T obj) {
        if (size == data.length) {
            T[] data2 = (T[]) new Object[data.length * 2];
            System.arraycopy(data, 0, data2, 0, data.length);
            data = data2;
        }
        data[size++] = obj;

    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator_ iterator() {
        return new ArrayListIterator_<T>();
    }

    private class ArrayListIterator_<T> implements Iterator_<T> {
        int index = 0;

        @Override
        public T next() {
            return (T)data[index++];
        }

        @Override
        public boolean hasNext() {
            return index < size;
        }
    }
}
